package com.hcb.micro.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcb.micro.controller.base.BaseControllers;
import com.hcb.micro.dto.AccountPlatform;
import com.hcb.micro.dto.Manager;
import com.hcb.micro.dto.UplevelLog;
import com.hcb.micro.service.IAccountPlatformService;
import com.hcb.micro.service.IUplevelLogService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("uplevelLog")
public class UplevelLogController extends BaseControllers{
	
	@Autowired
	IUplevelLogService uplevelLogService;
	
	@Autowired
	IAccountPlatformService accountPlatformService;
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public String listByType() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		Map<String,Object> para = new HashMap<>();
		
		List<UplevelLog> ullListAll = uplevelLogService.selectByPara(para);
		
		if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
			para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
			para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		List<UplevelLog> ullList = uplevelLogService.selectByPara(para);
		
		if(ullList.size() <= 0){
			json.put("result", "1");
			json.put("description", "列表不存在");
			return buildReqJsonObject(json);
		}
		List<Map<String,String>> list = new ArrayList<>();
		for(UplevelLog ull:ullList){
			Map<String,String> map = new HashMap<>();
			AccountPlatform ap = accountPlatformService.selectByAccountId(ull.getAccountId());
			//成为合伙人，支付补偿费状态（0.未激活；1.已激活，未支付；2.已支付；3.升级成功）
			String apValue = "";
			switch(ap.getBepartner()){
			case 0:
				apValue = "未激活";
				break;
			case 1:
				apValue = "已激活，未支付";
				break;
			case 2:
				apValue = "已激活，已支付";
				break;
			case 3:
				apValue = "已激活，升级成功";
				break;
			}
			map.put("apValue", apValue);
			map.put("bepartner", ap.getBepartner()+"");
			
			String isPayValue = "";
			switch(ull.getIsPay()){
			case 0:
				isPayValue = "未支付";
				break;
			case 1:
				isPayValue = "已支付";
				break;
			}
			
			if(ap.getPartnerAccountId()==0){
				map.put("havaPartner", "无");
				isPayValue = "无需支付";
			}else{
				map.put("havaPartner", "有");
			}
			map.put("partnerAccountId", ap.getPartnerAccountId()+"");
			
			map.put("id", ull.getId()+"");
			map.put("accountId", ull.getAccountId()+"");
			map.put("payMark", ull.getPayMark());
			map.put("payId", ull.getPayId()==null?"":ull.getPayId());
			map.put("payPrice", ull.getPayPrice()+"");
			
			map.put("isPay", ull.getIsPay()+"");
			map.put("isPayValue", isPayValue);
			map.put("createTime", sdf.format(ull.getCreateTime()));
			list.add(map);
		}
		
    	json.put("result", "0");
		json.put("description", "获取列表成功");
		json.put("list", list);
		json.put("count", ullListAll.size());
		return buildReqJsonObject(json);
	}
}
